#!/usr/bin/env python2.5
# encoding: utf-8
"""
SQLOperatorStream.py

Created by Alakazam on 2007-07-05.
Copyright (c) 2007 . All rights reserved.
"""

from SQLStream import *
from OperatorStream import *

class SQLOperatorStream(SQLStream, OperatorStream):
  def __init__(self, connection, name, idClause, startTimeClause, endTimeClause, valueClause, fromClause, whereClause):
    SQLStream.__init__(self, connection, name)
    
    self.streamTableStructure = """\
      SELECT %(idClause)s        AS id,\
             %(startTimeClause)s AS startTime,\
             %(endTimeClause)s   AS endTime,\
             %(valueClause)s     AS value\
        FROM %(fromClause)s\
       WHERE %(whereClause)s"""%(locals())
    
    self.DBServer.cursor.execute("CREATE VIEW `" + self.name + "` AS " + self.streamTableStructure)
  
  def delete(self):
    if self.streamTableStructure:
      self.DBServer.execute('DROP VIEW ' + self.name);
  
